﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;



namespace Common
{
    public static class CheckServerClient
    {
        public static Boolean SaveFile(string active, string nameSpace, string links, string iP, string ports)
        {
            Boolean check = false;
            try
            {
                string sFileName = @"\CheckServerClient.xml";
                string Links = System.Windows.Forms.Application.StartupPath.ToString() + sFileName;
                if (Common.Utilities.CheckFile(sFileName) == true)
                {
                    Common.Utilities.DeleteFile(sFileName);
                }
                XmlTextWriter txtwrite = new XmlTextWriter(Links, null);
                txtwrite.WriteStartDocument();
                txtwrite.WriteStartElement("ServerClient");
                txtwrite.WriteElementString("Active", Common.Utilities.Encrypt("SupermarketManagement", active));
                txtwrite.WriteElementString("NameSpace", Common.Utilities.Encrypt("SupermarketManagement", nameSpace));
                txtwrite.WriteElementString("Links", Common.Utilities.Encrypt("SupermarketManagement", links));
                txtwrite.WriteElementString("IP", Common.Utilities.Encrypt("SupermarketManagement", iP));
                txtwrite.WriteElementString("Ports", Common.Utilities.Encrypt("SupermarketManagement", ports));
                txtwrite.WriteEndElement();
                txtwrite.WriteEndDocument();
                txtwrite.Close();
                check = true;
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString(); check = false;
            }
            return check;
        }

        public static Boolean CreateFile()
        {
            Boolean kt = false;
            try
            {
                string sFileName = @"\CheckServerClient.xml";
                string Links = System.Windows.Forms.Application.StartupPath.ToString() + sFileName;
                if (Common.Utilities.CheckFile(sFileName) == true)
                {
                    Common.Utilities.DeleteFile(sFileName);
                }
                XmlTextWriter txtwrite = new XmlTextWriter(Links, null);
                txtwrite.WriteStartDocument();
                txtwrite.WriteStartElement("ServerClient");
                txtwrite.WriteElementString("Active", Common.Utilities.Encrypt("SupermarketManagement", "server"));
                txtwrite.WriteElementString("NameSpace", Common.Utilities.Encrypt("SupermarketManagement", "SupermarketManagement"));
                txtwrite.WriteElementString("Links", Common.Utilities.Encrypt("SupermarketManagement", "www.lamtransoft.com"));
                txtwrite.WriteElementString("IP", Common.Utilities.Encrypt("SupermarketManagement", "127.0.0.1"));
                txtwrite.WriteElementString("Ports", Common.Utilities.Encrypt("SupermarketManagement", "2011"));
                txtwrite.WriteEndElement();
                txtwrite.WriteEndDocument();
                txtwrite.Close();
                kt = true;
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                kt = false;
            }
            return kt;
        }

        public static Entities.CheckServerClient getServerClient()
        {
            Entities.CheckServerClient sql = new Entities.CheckServerClient();
            string sFileName = @"\CheckServerClient.xml";
            string Links = System.Windows.Forms.Application.StartupPath.ToString() + sFileName;
            try
            {
                XmlDocument doc = new XmlDocument();
                if (Common.Utilities.CheckFile(sFileName) == true)
                {
                    doc.Load(Links);
                    XmlElement root = doc.DocumentElement;
                    sql.Active = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[0].InnerText);
                    sql.NameSpace = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[1].InnerText);
                    sql.Links = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[2].InnerText);
                    sql.IP = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[3].InnerText);
                    sql.Ports = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[4].InnerText);
                }
                else
                {
                    sql = null;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                Common.Utilities.DeleteFile(Links);
                CreateFile();
                sql = null;
            }
            return sql;
        }
    }
}
